import { Component, OnInit, Output, EventEmitter, Input, ViewChild } from '@angular/core';
import { keyword } from '../../keyword.data';
import { appKeywordUrl } from '../../keyword.url';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { TrialTableComponent } from '../trial-table/trial-table.component';
import {appSynonymUrl} from '../../../synonym/synonym.url';
declare var $:any;
@Component({
  selector: 'app-trial-button',
  templateUrl: './trial-button.component.html'
})
export class TrialButtonComponent implements OnInit {

  @Input() inputCheckStatus;
  checkStatus : number = 1;
  keywordListVal : Array<object>;
  themeTagListVal : Array<object>;
  changeCheck : any = 1;
  
  @Input() status: any  = '';
  @Input() labelId: any  = '';
  @Input() searchData: any;

  @Output() onSubmit = new EventEmitter<void>();
  right: any = {
    "keyword": {},
    "keywordTrial": {},
    "keywordUnreviewed": {}
  };

  constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public request: CudTableService,
    public server: Service,
    private hintsService: HintsService
  ) {
    this.right = $.extend(this.right, server.getRight());
   }

  ngOnInit() {
    keyword.retrieval.checkStatus = this.inputCheckStatus;
    this.checkStatus = this.inputCheckStatus;
    this._themeTagList();
  }
  
  //展开搜索
  public opensearch: boolean = false;
  public opensearchtitle: string = "展开搜索";
  openSearch() {
    this.opensearch = !this.opensearch;
    if (!this.opensearch) {
      this.opensearchtitle = "展开搜索";
    } else {
      this.opensearchtitle = "关闭搜索";
    }
    //发射搜索框状态
    this.server.themeSeventEmit.emit(this.opensearch)
  }

  //搜索
  search() {
    keyword.retrieval.queryCriteria = this.searchData;
    keyword.retrieval.status = this.status;
    keyword.retrieval.labelId = this.labelId;
    this.onSubmit.emit();
  }
  //响应搜索
  changesearch() {
      this.search();
  }

  /**
   * 启用
   */
  _enable(){
    this._status(0);
  }

   /**
    * 禁用
    */
  _disable(){
    this._status(-1);
  }

  /**
   * 审核还是取消审核
   */
  changeCheckStatus(val:any){
    this.changeCheck = val;
  }

  /**
   * 审核或取消审核
   */
  _review(val:any){
    this.removeTableService
    .confirmEnableDisable(keyword.retrieval.token,'ids',this.server.baseUrl +appKeywordUrl.updateThemeWordsCheckStatus,'checkStatus',val)
    .then(res => {
      if(res.result_code == "0000"){
        this.onSubmit.emit();
      }
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this.hintsService.messages('error','error');
      return;
    });
  }
  
  /**
   * 批量启用或禁用
   * @param val
   */
  private _status(val:any){

    this.removeTableService
    .confirmEnableDisable(keyword.retrieval.token,'ids',this.server.baseUrl +appKeywordUrl.updateThemeWordsStatus,'status',val)
    .then(res => {
      if(res.result_code == "0000"){
        this.onSubmit.emit();
      }
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this.hintsService.messages('error','error');
      return;
    });
  }

  /**
   * 点击新增按钮加载
   */
  _addList(){
    this.server.onParentKeyworkTabListEmit.emit();
  }

  /**
   * 点击发布主题词按钮加载
   */
  _clickExport(){
    this.server.onExportKeyworkEmit.emit();
  }

  /**
   * 点击下载主题词按钮加载
   */
  _clickDownload(){
    this.server.onDownloadKeyworkEmit.emit();
  }
  /**
   * 获取主题词标签列表
   */
  _themeTagList(){
    let data = {
      token         : keyword.retrieval.token,
      pageNo        : 1,
      pageSize      : 999,
    };
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getThemeWordsLabelList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.themeTagListVal = res.result;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }
  
  //下载主题词模板
  public download(){
    window.open("assets/template/themewords_template.xlsx");
  }

}
